#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: reset_crawl_flag.py
@time: 2018/1/8 19:18
"""

import sys

sys.path.append('..')
sys.path.append('../..')

from common.mongo import MongDb
from logger import Logger

APP_DATA_CONF = {
    'host': '172.16.215.16',
    'port': 40042,
    'db': 'app_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('reset_crawl_flag.log').get_logger()

app_data_db = MongDb(APP_DATA_CONF['host'], APP_DATA_CONF['port'], APP_DATA_CONF['db'],
                     APP_DATA_CONF['username'],
                     APP_DATA_CONF['password'], log=log)


def main():
    source_table_name = 'enterprise_data_gov_new_2018_01_08'

    count = 0
    result_list = []
    for item in app_data_db.traverse_batch_field(source_table_name, {'is_new_gsxt': False}, ['_id']):
        count += 1
        _id = item.get('_id')

        data = {
            '_id': _id,
            'is_new_gsxt': True,
        }
        result_list.append(data)
        if len(result_list) >= 500:
            app_data_db.insert_batch_data(source_table_name, result_list)
            del result_list[:]

        if count % 1000 == 0:
            log.info("当前设置进度: count = {}".format(count))

    if len(result_list) > 0:
        app_data_db.insert_batch_data(source_table_name, result_list)
        del result_list[:]
    log.info("设置完成...")


if __name__ == '__main__':
    main()
